package com.sy.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.sy.dto.Result;
import com.sy.util.AliOssCloudUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author:cxl
 * @Date: 2021/10/13 18:00
 */
@RestController
public class FileUploadController {

    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/file/upload", method = RequestMethod.POST)
    public Result upload(@RequestParam("file") MultipartFile file) {
        Result result = new Result();
        String filename = file.getOriginalFilename();
        String[] split = filename.split("\\.");
        String substring=split[split.length-2];
        DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
        String format = df.format(new Date());
        String name = substring +format+ filename.substring(filename.lastIndexOf("."), filename.length());
        name = name.replace("-", "");
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        AliOssCloudUtil util = new AliOssCloudUtil();
        //上传成功返回完整路径的url
        String url = util.uploadFile2OSS(inputStream, name);
        result.setData(url);
        return result;
    }

    /**
     * 判断文件是否存在
     * @param fileName  储存的文件名
     * @return
     */
    @RequestMapping(value = "/file/exists", method = RequestMethod.GET)
    public Boolean exists(String fileName) {
        AliOssCloudUtil util = new AliOssCloudUtil();
        Boolean bool = util.doesObjectExist(fileName);
        return bool;
    }


    /**
     * 下载oss对应文件
     * @param fileName 储存的文件名
     * @return
     */
    @RequestMapping(value = "/file/downLoad", method = RequestMethod.POST)
    public void downloadFile(@RequestParam("fileName") String fileName) {
        AliOssCloudUtil ossClientUtil = new AliOssCloudUtil();
        String myFileName = fileName.split(".com/")[fileName.split(".com/").length-1];
        String trueName=fileName.split("/")[fileName.split("/").length-1];
        ossClientUtil.downloadFile(myFileName,trueName);
    }

    /**
     * 删除oss对应文件
     * @param fileName 储存的文件名
     * @return
     */
    @RequestMapping(value = "/file/delet", method = RequestMethod.POST)
    public void deletloadFile(@RequestParam("fileName")String fileName) {

         String endpoint = "oss-cn-shanghai.aliyuncs.com";

        //阿里云的accessKeyId
         String accessKeyId = "LTAI5tE7fV9nKJsCw8Xt4TuU";

        //阿里云的accessKeySecret

         String accessKeySecret = "q91c4nizyWrVqnRg3nz4ZonvAmsadu";

        //空间
         String bucketName = "xiaoxiaoqinglong";
        // 填写文件完整路径。文件完整路径中不能包含Bucket名称。

        String objectName = fileName;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 删除文件或目录。如果要删除目录，目录必须为空。
        ossClient.deleteObject(bucketName, objectName);

         // 关闭OSSClient。
        ossClient.shutdown();

    }



}
